package com.xing.adapter;

import java.util.List;

import com.xing.controller.DBController;
import com.xing.fragment.SurveyListSecondFragment;
import com.xing.model.HsdModelSurvey;
import com.xing.my_model.SurveyListEntity;
import com.xing.questionnairesurvey.R;
import com.xing.util.FragmentNameFlagList;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SurveyListAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<HsdModelSurvey> mList;
	private Context mContext;
	private FragmentManager fragmentManager;
	
	public SurveyListAdapter(Context context,List<HsdModelSurvey> list,FragmentManager frmg){
		this.inflater = LayoutInflater.from(context);
		this.mList = list;
		this.mContext = context;
		this.fragmentManager = frmg;
	}
	
	public void setItemList(List<HsdModelSurvey> itemList) {
		this.mList = itemList;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		if(mList != null){
			return mList.size();
		}
		return 0;
	}

	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		if(mList != null){
			return mList.get(arg0);
		}
		return null;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		if(mList != null){
			return position;
		}
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
			final HsdModelSurvey object  = mList.get(position);
//			ViewHolder holder;
			if(convertView == null){
//				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.survey_listview_item, null);
//				holder.titleTextView = (TextView) convertView.findViewById(R.id.questionnaire_name);
//				holder.hasUploadTextView = (TextView) convertView.findViewById(R.id.has_done_number);
//				holder.notUploadTextView = (TextView) convertView.findViewById(R.id.not_done_number);
				
				
//				convertView.setTag(holder);
			}else
			{
//				holder = (ViewHolder) convertView.getTag();
			}
			
		
		TextView titleTextView = (TextView) convertView.findViewById(R.id.questionnaire_name);
		TextView hasFnishedTextView = (TextView) convertView.findViewById(R.id.has_done_number);
		TextView notFinishedTextView = (TextView) convertView.findViewById(R.id.not_done_number);
		
		
		titleTextView.setText(object.getName());
		DBController.getInstance().getSurveyListEntity(mContext, object.getSurveyuuid(), hasFnishedTextView, notFinishedTextView, handler);
		
		
		convertView.findViewById(R.id.start_survey).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
				fragmentTransaction.hide(fragmentManager.findFragmentByTag(FragmentNameFlagList.SURVEY));
				SurveyListSecondFragment surveySecondFragment = new SurveyListSecondFragment(); 
				Bundle bundle = new Bundle();
				/**
				 * 调查问卷的id
				 */
				bundle.putString("surveyuuid", object.getSurveyuuid());
				bundle.putString("survey_name", object.getName());
				surveySecondFragment.setArguments(bundle);
				fragmentTransaction.add(R.id.fragmentRoot, surveySecondFragment, FragmentNameFlagList.SURVEY_SECOND_FRAGMENT);
				fragmentTransaction.addToBackStack(FragmentNameFlagList.SURVEY_SECOND_FRAGMENT);
				fragmentTransaction.commit();
			}
		});
		return convertView;
	}
	
	
//	static class ViewHolder{
//		TextView titleTextView;
//		TextView hasUploadTextView;
//		TextView notUploadTextView;
//	}
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			SurveyListEntity surveyListEntity = (SurveyListEntity) msg.obj;
			((TextView)surveyListEntity.getHasFinishedView()).setText(String.valueOf(surveyListEntity.getHasFinished()));
			((TextView)surveyListEntity.getNotFinishedView()).setText(String.valueOf(surveyListEntity.getNotFinished()));
			
		}
	};

}
